<template>
  <div class="roles_container">
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
      <el-breadcrumb-item>角色详情</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table border stripe :data="rolesList" style="width: 100%">
        <el-table-column type="expand" class="space">
          <template slot-scope="scope">
            <el-row v-for="(item1,i1) in scope.row.children" :key="item1.id" :class="['bdbottom','v-center',i1 === 0 ? 'bdtop':'']">
              <!-- 一级 -->
              <el-col :span="5">
                <el-tag closable>{{item1.authName}}</el-tag><i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级与三级 -->
              <el-col :span="19">
                <el-row v-for="(item2,i2) in item1.children" :key="item2.id" :class="[i2 === 0 ? '':'bdbottom']">
                  <!-- 二级 -->
                  <el-col :span="6">
                    <el-tag closable type="success">{{item2.authName}}</el-tag><i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级节点 -->
                  <el-col :span="18">
                    <el-tag v-for="item3 in item2.children" :key="item3.id" closable type="warning">{{item3.authName}}</el-tag><i class="el-icon-caret-right"></i></el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述">
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRights(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="rightsdialogVisible" @close="rightsDialogClose">
      <el-tree ref="treeRef" :data="rightList" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys"></el-tree>
      <div slot="footer">
        <el-button @click="rightsdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="alloRights">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'RolesPage',
  data () {
    return {
      rolesList: [],
      rightsdialogVisible: false,
      rightList: [],
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [],
      roleId: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    // 获取角色列表
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败！')
      this.rolesList = res.data
    },
    // 获取树形权限列表
    async getRightsList () {
      const { data: res } = await this.$http.get('rights/tree')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败！')
      this.rightList = res.data
    },
    showSetRights (role) {
      // 打开对话框时赋值id 为后续分配做准备
      this.roleId = role.id
      this.getRightsList()
      this.rightsdialogVisible = true
      this.getLeafKeys(role, this.defKeys)
    },
    // 获取当亲角色默认节点
    getLeafKeys (node, arr) {
      // 三级节点直接放进数组，作为默认
      if (!node.children) return arr.push(node.id)
      // 不是三级要继续进行遍历
      // 递归调用自身
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    // 分配权限对话框关闭
    rightsDialogClose () {
      this.defKeys = [] // 否则内容会一直叠加
    },
    // 分配权限接口调用--角色授权
    async alloRights () {
      // roles/:roleId/rights
      // 获取选中和半选中节点数组
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const keysStr = keys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: keysStr })
      if (res.meta.status !== 200) return this.$message.error('操作失败，请稍后重试！')
      this.getRolesList()
      this.rightsdialogVisible = false
      this.$message.success('分配权限成功！')
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 8px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.v-center {
  display: flex;
  align-items: center;
}
.space {
  padding: 0 20px;
}
</style>
